package com.life.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.Collection;
public class CommUtil {
	
	/** 
	 * 先MD5再BASE64加密
	 * */
/*	public static String md5Base64Str(String str){
		MessageDigest md=null;
		try{
			md=MessageDigest.getInstance("MD5");
		}catch(Exception e){
			e.printStackTrace();
		}
		int i;
		byte b[]=md.digest(str.getBytes());
		StringBuffer buf = new StringBuffer(""); 
			for (int offset = 0; offset < b.length; offset++) { 
			i = b[offset]; 
			if(i<0) i+= 256; 
			if(i<16) 
			buf.append("0"); 
			buf.append(Integer.toHexString(i)); 
		} 	
		String md5Base64=Base64.encode(buf.toString().getBytes()).toString();
		return md5Base64;
		//	System.out.println("result: " + buf.toString()+"---length:"+buf.toString().length());
		//	System.out.println("b64:"+md5Base64+"---length:"+md5Base64.length()+",,,,"+"OTg4ODM1ZDUxNjBkZmVkZjU3MjVkZTA3M2NmMzJkYmI=".equals(md5Base64));
	}*/
	
	

	
	
	
	
	public static Integer convertToInt(Object o){
		Integer res=0;
		if(o instanceof BigInteger){
			BigInteger bi=(BigInteger)o;
			res=bi.intValue();
		}
		if(o instanceof Long){
			Long lo=(Long)o;
			res=lo.intValue();
		}
		if(o instanceof Integer){
			return ((Integer) o).intValue();
		}
		return res;
	}
	

	

	
	
	/**
	 * 四舍五入保留小数
	 * */
	public static Double ConvertToDoubleRound(Object obj, int scale) {
		double num = 0d;
		String str = "0";
		if (obj!=null) {
			str = obj.toString().trim();
		}
		num = ConvertToBigDec(str).setScale(scale, RoundingMode.HALF_UP).doubleValue();
		return num;
	}
	
	/**
	 * 取小数点后几位小数
	 * */
	public static Double ConvertToDoubleRoundDown(Object obj, int scale) {
		double num = 0d;
		String str = "0";
		if (obj!=null) {
			str = obj.toString().trim();
		}
		num = ConvertToBigDec(str).setScale(scale, RoundingMode.DOWN).doubleValue();
		return num;
	}
	
	public static BigDecimal ConvertToBigDec(Object obj) {
		if (obj==null)
			return new BigDecimal("0.00");
		else {
			String str = obj.toString().trim();
			if (str == null || str == "")
				return new BigDecimal("0.00");
			else
				return new BigDecimal(str.trim());
		}
	}
	
	 
	 public static String makeArgs(int argsLength){
		 StringBuffer args  = new StringBuffer();
		 for (int i = 0; i < argsLength; i++) {
			 args.append("?,");
		 }
		 String returnArgs = "";
		 if(!args.toString().equals("")){
			 returnArgs = args.toString().substring(0, args.toString().length()-1);
		 }
		 return returnArgs;
	 }
}
